package com.chzc.fm.duotin.ui.channel;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.adapter.recyclerview.RankLinearLayoutAdapter;
import com.chzc.fm.duotin.bean.channel.Rank;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.GlobalParam;
import com.chzc.fm.duotin.utils.HttpUtil;
import com.chzc.fm.duotin.utils.ImageUtil;
import com.chzc.fm.duotin.utils.LogUtil;
import com.chzc.fm.duotin.widget.SpacesItemDecoration;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2016/6/27.
 */
public class RankActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvlisten,tvbook,tvcommend;
    private ImageView ivlisten,ivbook,ivcommend,ivback;
    private LinearLayout lllisten,llbook,llcomment;
    private RecyclerView recyclerView ;
    private RankLinearLayoutAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rank);

        initViews();

        initDatas();
    }

    public void initViews()
    {
        tvlisten = (TextView) findViewById(R.id.rank_listen_tv);
        tvbook = (TextView) findViewById(R.id.rank_book_tv);
        tvcommend = (TextView) findViewById(R.id.rank_comment_tv);

        ivlisten = (ImageView) findViewById(R.id.rank_listen_iv);
        ivbook = (ImageView) findViewById(R.id.rank_book_iv);
        ivcommend  = (ImageView) findViewById(R.id.rank_comment_iv);
        ivback = (ImageView) findViewById(R.id.rank_back_iv);

        lllisten = (LinearLayout) findViewById(R.id.rank_listen_ll);
        llbook = (LinearLayout) findViewById(R.id.rank_book_ll);
        llcomment = (LinearLayout) findViewById(R.id.rank_comment_ll);

        recyclerView = (RecyclerView) findViewById(R.id.rank_special_rv);

        LinearLayoutManager manager = new LinearLayoutManager(getApplicationContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        recyclerView.addItemDecoration(new SpacesItemDecoration(50));


        ivback.setOnClickListener(this);
        lllisten.setOnClickListener(this);
        llbook.setOnClickListener(this);
        llcomment.setOnClickListener(this);


    }

    public void initDatas()
    {
        getRank(new DuotinTask.IRequestCallback() {
            @Override
            public void success(Object object) {

                try {

                    LogUtil.w("开始解析数据");
                    JSONObject root = new JSONObject(object.toString());
                    String error_msg = root.getString("error_msg");
                    if("success".equals(error_msg))
                    {
                        JSONArray data = root.getJSONArray("data");

                        showHotRank(data);

                        showSpecial(data);


                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    /**
     *
     * 显示热门榜单视图
     * @param data
     * @throws JSONException
     */
    public void showHotRank(JSONArray data) throws JSONException {
        JSONObject object1 = data.getJSONObject(0);
        JSONArray data_list = object1.getJSONArray("data_list");
        List<Rank> rankHotList = Rank.arrayRankFromData(data_list.toString());

        tvlisten.setText(rankHotList.get(0).getTitle());
        ImageLoader.getInstance().displayImage(rankHotList.get(0).getImage_url().toString(),ivlisten, ImageUtil.getDefaultOptions());

        tvbook.setText(rankHotList.get(1).getTitle());
        ImageLoader.getInstance().displayImage(rankHotList.get(1).getImage_url().toString(),ivbook, ImageUtil.getDefaultOptions());

        tvcommend.setText(rankHotList.get(2).getTitle());
        ImageLoader.getInstance().displayImage(rankHotList.get(2).getImage_url().toString(),ivcommend, ImageUtil.getDefaultOptions());
    }


    /**
     * 显示专题精榜视图
     * @param data
     * @throws JSONException
     */
    public void showSpecial(JSONArray data) throws JSONException {

        JSONObject object2 = data.getJSONObject(1);
        JSONArray data_list = object2.getJSONArray("data_list");

        final List<Rank> speciallist = Rank.arrayRankFromData(data_list.toString());

        LogUtil.w("speciallist.size() = " + speciallist.size());

        adapter  = new RankLinearLayoutAdapter(getApplicationContext(),speciallist);
        recyclerView.setAdapter(adapter);

        adapter.setItemOnClickListener(new RankLinearLayoutAdapter.mItemOnClickListener() {
            @Override
            public void OnItemClick(int position) {

                switch (position)
                {
                    default:
                        Intent intent = new Intent(RankActivity.this,SpecialRankActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("specialitem",speciallist.get(position));
                        intent.putExtras(bundle);
                        startActivity(intent);
                        break;
                }
            }
        });
    }




    /**
     * 请求频道模块的榜单的数据
     * @param callback
     */
    public void getRank(DuotinTask.IRequestCallback callback)
    {
        DuotinTask.IRequest request = new DuotinTask.IRequest() {
            @Override
            public Object doRequest() {
                return HttpUtil.doGet(GlobalParam.URL_RANK);
            }
        };

        new DuotinTask(request,callback).execute();
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId())
        {
            case R.id.rank_back_iv:
                finish();
                break;

            case R.id.rank_listen_ll:
                intent = new Intent(RankActivity.this,ListenRankActivity.class);
                startActivity(intent);
                Toast.makeText(RankActivity.this,"你点击的是收听榜",Toast.LENGTH_SHORT).show();
                break;
            case R.id.rank_book_ll:
                intent = new Intent(RankActivity.this,BookRankActivity.class);
                startActivity(intent);
                Toast.makeText(RankActivity.this,"你点击的是订阅榜",Toast.LENGTH_SHORT).show();
                break;
            case R.id.rank_comment_ll:
                intent = new Intent(RankActivity.this,CommentRankActivity.class);
                startActivity(intent);
                Toast.makeText(RankActivity.this,"你点击的是评论榜",Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
